// pages/todo/detail.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		activity: null,
		token: getApp().globalData.token,
		users: [],
		msgs: [],
		cursor: 0,
		isQuery: false,
		isQueryMsgs: false,
	},


	/**
	 * 关注活动
	 */
	attendActivity() {
		console.log("查询关注列表 token = ", this.data.token)
		wx.request({
			url: 'https://marklife.love/5h4h/attend/activity',
			method: 'POST',
			//传参使用form-data格式
			header: {
				'content-type': 'application/x-www-form-urlencoded' //修改此处即可
			},
			data: {
				token: this.data.token,
				activity: this.data.activity.key,
				add: true,
			},
			success: (res) => {
				console.log("attend activity: ", res.data)
				var errCode = res.data.err
				if (errCode == 1000) {
					wx.hideLoading()
					//新增成功
					//新增成功提示
					wx.showModal({
						title: '恭喜您',
						icon: "success",
						content: '参加活动成功',
						showCancel: false,
						complete: (res) => {
							if (res.confirm) {
								this.subscribeMessage()
								this.setData({
									cursor:0,
								})
								this.getActivityFollowers()
							}
						}
					})
				} else {
					//新增失败 未知原因
					wx.hideLoading()
					wx.showModal({
						title: '关注活动失败',
						content: '抱歉，关注活动失败，我也没时间去排查错误原因：' + errCode,
						showCancel: false,
						complete: (res) => {
							if (res.cancel) {}
							if (res.confirm) {}
						}
					})
				}
			}
		})
	},

	
	//申请授权
	subscribeMessage() {
		wx.requestSubscribeMessage({
			tmplIds: ['yxrr1WvLBJdHD1un8jsGC8sdiEiWG9vyHa1jRqhlMsI'], //这里填入我们生成的模板id
			success(res) {
				console.log('授权成功', res)
			},
			fail(res) {
				console.log('授权失败', res)
			}
		})
	},

	/**
	 * 获取关注活动用户列表
	 */
	getActivityMsgs() {
		if (this.data.isQueryMsgs) return;
		this.setData({
			isQueryMsgs: true,
		})
		console.log("查询留言板 token = ", this.data.token)
		wx.request({
			url: 'https://marklife.love/5h4h/get/activity/msg',
			method: 'POST',
			//传参使用form-data格式
			header: {
				'content-type': 'application/x-www-form-urlencoded' //修改此处即可
			},
			data: {
				token: this.data.token,
				activity: this.data.activity.key,
				limit: 20,
				cursor: this.data.cursor,
			},
			complete: () => {
				this.setData({
					isQueryMsgs: false
				})
			},
			success: (res) => {
				var errCode = res.data.err
				if (errCode == 1000) {
					//查询留言板列表成功
					console.log("查询留言板成功", res.data.data)

					if (res.data.data.length == 0) {
					}
			
					if (this.data.cursor == 0) {
						this.setData({
							msgs: res.data.data,
							cursor: res.data.cursor
						})
					} else {
						var temp = [...this.data.msgs, ...res.data.data]
						this.setData({
							msgs: [...this.data.msgs, ...res.data.data],
							cursor: res.data.cursor
						})
					}

					wx.stopPullDownRefresh()
					wx.hideLoading()
				} else {
					wx.stopPullDownRefresh()
					wx.hideLoading()
					//查询留言板失败 未知原因
					// wx.showModal({
					// 	title: '查询失败',
					// 	content: '抱歉，查询失败，我也没时间去排查错误原因',
					// 	showCancel: false,
					// 	complete: (res) => {
					// 		if (res.cancel) {}
					// 		if (res.confirm) {}
					// 	}
					// })
				}
			},
			fail: (res) => {
				wx.stopPullDownRefresh()
				wx.hideLoading()
				wx.showModal({
					title: '查询失败',
					content: '抱歉，查询失败，我也没时间去排查错误原因',
					showCancel: false,
					complete: (res) => {
						if (res.cancel) {}
						if (res.confirm) {}
					}
				})
			}
		})
	},

	/**
	 * 获取关注活动用户列表
	 */
	getActivityFollowers() {
		if (this.data.isQuery) return;
		this.setData({
			isQuery: true,
		})
		console.log("查询关注列表 token = ", this.data.token)
		console.log("查询关注列表 cursor = ", this.data.cursor)
		wx.request({
			url: 'https://marklife.love/5h4h/get/activity/followers',
			method: 'POST',
			//传参使用form-data格式
			header: {
				'content-type': 'application/x-www-form-urlencoded' //修改此处即可
			},
			data: {
				token: this.data.token,
				activity: this.data.activity.key,
				limit: 20,
				cursor: this.data.cursor,
			},
			complete: () => {
				this.setData({
					isQuery: false
				})
			},
			success: (res) => {
				var errCode = res.data.err
				if (errCode == 1000) {
					//查询关注活动用户列表成功
					console.log("查询成功", res.data.data)

					if (res.data.data.length == 0) {
					}
			
					if (this.data.cursor == 0) {
						this.setData({
							users: res.data.data,
							cursor: res.data.cursor
						})
					} else {
						var temp = [...this.data.users, ...res.data.data]
						this.setData({
							users: [...this.data.users, ...res.data.data],
							cursor: res.data.cursor
						})
					}

					wx.stopPullDownRefresh()
					wx.hideLoading()
				} else {
					wx.stopPullDownRefresh()
					wx.hideLoading()
					//查询活动列表失败 未知原因
					wx.showModal({
						title: '查询失败',
						content: '抱歉，查询失败，我也没时间去排查错误原因',
						showCancel: false,
						complete: (res) => {
							if (res.cancel) {}
							if (res.confirm) {}
						}
					})
				}
			},
			fail: (res) => {
				wx.stopPullDownRefresh()
				wx.hideLoading()
				wx.showModal({
					title: '查询失败',
					content: '抱歉，查询失败，我也没时间去排查错误原因',
					showCancel: false,
					complete: (res) => {
						if (res.cancel) {}
						if (res.confirm) {}
					}
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log("select activity: ", options.obj)
		let obj = JSON.parse(options.obj)
		let token=options.token
		console.log("select token: ",token)
		this.setData({
			activity: obj,
			token:token,
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.setData({
			signature: getApp().globalData.signature,
			token: getApp().globalData.token,
			nickName: getApp().globalData.nickName,
			openId: getApp().globalData.openId,
			avatarUrl: getApp().globalData.avatarUrl,
		})
		if (!this.data.isQuery) {
			this.getActivityFollowers()
			this.getActivityMsgs()
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		//如果是下拉刷新，那么就要把指针回到0
		this.setData({
			cursor: 0,
		})
		if (this.token != null) {
			this.getActivityFollowers()
			return;
		}
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})